import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import {ElMessage} from "element-plus";

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'home',
    component: () => import('../views/HomeView.vue'),
    redirect: '/total',
    children: [
      {path: '/total', component: () => import('../views/total/TotalView.vue')},
      {path: '/graph', component: () => import('../views/graphs/GraphView.vue')},
      {path: '/present', component: () => import('../views/hotpointList/PresentView.vue')},
      {path: '/history', component: () => import('../views/hotpointList/HistoryView.vue')}
    ]
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView.vue')
  },
  {
    path: '/test',
    name: 'test',
    component: () => import('../views/test/test.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  let user = localStorage.getItem('user')

  if (to.path != '/login' && !user && to.path != '/') {
    ElMessage.error('请先登录')
    next('/login')
  } else {
    next()
  }
})

export default router
